#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import base64
import hmac
import time


def get_token(key, expire=3600):
    '''
    :param key: str (用户给定的key，需要用户保存以便之后验证token,每次产生token时的key 都可以是同一个key)
    :param expire: int(最大有效时间，单位为s)
    :return: token
    '''
    ts_str = str(time.time() + expire)
    ts_byte = ts_str.encode("utf-8")
    sha1_tshexstr = hmac.new(key.encode("utf-8"), ts_byte, 'sha1').hexdigest()
    token = ts_str + ':' + sha1_tshexstr
    b64_token = base64.urlsafe_b64encode(token.encode("utf-8"))
    return b64_token.decode("utf-8")


def out_token(key, token):
    '''
    :param key: 服务器给的固定key
    :param token: 前端传过来的token
    :return: true,false
    '''

    # token是前端传过来的token字符串
    try:
        token_str = base64.urlsafe_b64decode(token).decode('utf-8')
        token_list = token_str.split(':')
        if len(token_list) != 2:
            return False
        ts_str = token_list[0]
        if float(ts_str) < time.time():
            # token expired
            return False
        saved_sha1 = token_list[1]  # 数据库中存储的token
        generate_sha1 = hmac.new(key.encode("utf-8"), ts_str.encode('utf-8'), 'sha1')  # 根据登录状态新生成的token
        # print(sha1)
        encoding_sha1 = generate_sha1.hexdigest()
        if encoding_sha1 != saved_sha1:
            # 验证失败
            return False
        # 验证成功
        return True
    except Exception as e:
        print(e)


def is_login(request):
    username = request.POST.get('username')
    token = request.POST.get('token')
    token_obj = out_token(username, token)
    if token_obj:
        return True
    else:
        return False
